//
//  WBHomeViewController.swift
//  MyWeibo
//
//  Created by timw on 2017/3/16.
//  Copyright © 2017年 wuqc. All rights reserved.
//

import UIKit
// 定义全局常量，尽量使用private修饰，否则到处都可以访问
private let cellId = "cellId"

class WBHomeViewController: WBBaseViewController {

    /// 列表视图模型
    fileprivate lazy var listViewModel = WBStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    /// 加载数据
    override func loadData() {
        
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess, shouldRefresh) in
            print("加载数据结束\(self.listViewModel.statusList.last?.text)")
            // 结束刷新控件
            self.refreshControl?.endRefreshing()
            // 恢复上拉刷新标记
            self.isPullup = false 
            // 刷新表格
            if shouldRefresh {
                self.tableView?.reloadData()
            }
            
        }
    }
    
    @objc fileprivate func showFriends() {
        print(#function)
        let vc = WBDemoViewController()
        //vc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(vc, animated: true)
    }

}

// MAEK: - 设置界面
extension WBHomeViewController {

    /// 重写父类的方法
    override func setupTableView() {
        super.setupTableView()
        
        // 设置导航栏按钮
        //        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        // 注册原型 cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }

}

// MARK: - 表格数据源方法，具体的数据源方法实现，不需要 super
extension WBHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 1. 取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        // 2. 设置cell
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        // 3. 返回cell
        return cell
    }
}
